<?php

class Default_Models_Image extends Libs_Model {

    private $img_id;
    private $url;
    private $pro_id;

    public function setImgId($img_id) {
        $this->img_id = $img_id;
    }

    public function getImgId() {
        return $this->img_id;
    }

    public function setUrl($url) {
        $this->url = $url;
    }

    public function getUrl() {
        return $this->url;
    }

    public function setProId($pro_id) {
        $this->pro_id = $pro_id;
    }

    public function getProId() {
        return $this->pro_id;
    }

    protected function setValue($row, $isKey = true) {
        $image = new Default_Models_Image();
        if ($isKey) {
            $image->setImgId($row['img_id']);
        }
        $image->setUrl($row['url']);
        $image->setProId($row['pro_id']);
        return $image;
    }

    protected function getKeyAndValue(Default_Models_Image $image, $isKey = false) {
        $data = array();
        if ($isKey) {
            $data['img_id'] = $image->getImgId();
        }
        $data['url'] = $image->getUrl();
        $data['pro_id'] = $image->getProId();
        return $data;
    }

    public function __construct() {
        parent::__construct();
    }

    public function insertImages($img) {
        $this->db->prepareInsert($img);
        $this->db->insert("tbl_images");
    }

    public function deleteImage($img_id) {
        $sql = "delete from tbl_images where img_id = $img_id";
        $this->db->query($sql);
    }

    public function getImages($pro_id) {
        if (isset($pro_id)) {
            $sql = "select * from tbl_images where pro_id = $pro_id";
            $result = $this->db->fetchAllO($sql);
            while ($rows = mysql_fetch_assoc($result)) {
                $out[] = $this->setValue($rows);
            }
            return $out;
        }
    }

}
